//
//  BidDetailViewController.swift
//  HuaYun
//
//  Created by Mac on 2019/3/19.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class BidDetailViewController: PagedViewController {

    var id = ""
    
    private var _needapproval = true
    var needapproval: Bool{
        get{
            return _needapproval
        }
        set{
            _needapproval = newValue
            if _needapproval {
                
            } else {
                
            }
        }
    }
    
    let approvalBtn = UIButton.init(type: .system)
    
    @objc func approvalAction(button: UIButton){
        let approval = ApprovalSuggestionViewController()
        
        approval.applyId = self.id
        
        approval.netPath = BidApprovalAction
        
        approval.businessType = "140"
        
        self.navigationController?.present(vc: approval)
//        self.present(vc: approval)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(approvalBtn)
        approvalBtn.setTitle("审批意见", for: .normal)
        
        approvalBtn.setTitleColor(UIColor.white, for: .normal)
        
        approvalBtn.setTitleColor(UIColor.hex("999999"), for: .disabled)
        
        approvalBtn.backgroundColor = UIColor.mainColor
        
        approvalBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        
        approvalBtn.addTarget(self, action: #selector(approvalAction(button:)), for: .touchUpInside)
        
        
        
        self.approvalBtn.snp.updateConstraints { (make) in
            make.height.equalTo(55)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        self.page.view.snp.makeConstraints { (make) in
            make.bottom.equalTo(approvalBtn.snp.top)
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(55)
            make.width.equalToSuperview()
        }
        self.load(with: "Detail")
        NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: Notification.Name("RefreshDetail"), object: nil)
//        NotificationCenter.default.post(name: NSNotification.Name("RefreshDetail"), object: nil)
    }
    
    @objc func refresh(){
//        self.load(with: "Detail")
        self.pop()
    }
    
    override func path(for name: String) -> String {
        return BidApprovalDetail
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return ["id": self.id]
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            self.endLoading()
            if json["success"].bool == true {
                var contents: [Dictionary<String,String?>] = []
                let operation = json["data"]["operation"].bool ?? false
                if operation == true {
                    self.approvalBtn.snp.updateConstraints { (make) in
                        make.height.equalTo(55)
                        make.left.equalToSuperview()
                        make.right.equalToSuperview()
                        make.bottom.equalToSuperview()
                    }
                } else {
                    self.approvalBtn.snp.updateConstraints { (make) in
                        make.height.equalTo(0)
                        make.left.equalToSuperview()
                        make.right.equalToSuperview()
                        make.bottom.equalToSuperview()
                    }
                }
                contents.append(["关联潜在项目:": json["data"]["details"].string])
                let projectBudget = json["data"]["potentialProjectVO"]["projectBudget"].int ?? 0
                contents.append(["项目预算(元):": String(projectBudget / 100)])
                contents.append(["销售负责人:": json["data"]["details"]["salesDirector"].string])
                contents.append(["技术负责人:": json["data"]["details"]["technicalDirector"].string])
                contents.append(["重要性:": json["data"]["details"]["importance"].string])
                contents.append(["客户单位:": json["data"]["details"]["customerUnit"].string])
                contents.append(["招标代理机构:": json["data"]["details"]["tenderingAgency"].string])
                contents.append(["投标日期:": json["data"]["details"]["biddingDate"].string])
                contents.append(["申请人:": json["data"]["details"]["applyer"].string])
                contents.append(["申请部门:": json["data"]["details"]["applyDept"].string])
                contents.append(["申请日期:": json["data"]["details"]["applyDate"].string])
                contents.append(["项目内容:": json["data"]["details"]["projectContent"].string])
                let weather = json["data"]["details"]["whetherMeetApproves"].bool ?? false
                if weather {
                    contents.append(["是否有指定功用上(品牌或型号)需要报招投标领导小组会议审核:": "是"])
                } else {
                    contents.append(["是否有指定功用上(品牌或型号)需要报招投标领导小组会议审核:": "否"])
                }
                let first = self.ctls.first as! PurchaseTableViewController
                first.source = contents
                first.documents = json["data"]["details"]["attachFiles"].array ?? []
                first.tableView.reloadData()
                let approvalList = json["data"]["approvalList"].array ?? []
                let second = self.ctls[1] as! ApprovalRecordListViewController
                second.source = approvalList
                second.tableView.reloadData()
            } else {
                self.alert(json["errorMsg"].string)
            }
        }
        return suc
    }
    
    override func method(for name: String) -> NetMethod {
        return .get
    }
    
    override func configTitles() -> Array<String> {
        return ["基本信息","审批记录"]
    }
    
    override func configViewControllers() -> [UIViewController] {
        let baseinfo = PurchaseTableViewController()
        baseinfo.filePath = .Finacial
        return [baseinfo,ApprovalRecordListViewController()]
    }

}
